<template>
	<div class="content">
		<div class="content-header">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>系统监控</el-breadcrumb-item>
				<el-breadcrumb-item>
					<b>系统日志</b>
				</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="content-body">
			<el-scrollbar>
				<div class="panel">
					<el-form :model="sysLogForm" ref="sysLogForm" inline label-width="100px">
						<el-row>
							<el-col :md="8">
								<el-form-item label="操作类型">
									<el-select v-model="sysLogForm.operateType" placeholder="全部">
										<el-option v-for="item in operateTypeList" :key="item.id" :label="item.name"
											:value="item.id">
										</el-option>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :md="8">
								<el-form-item label="接口地址">
									<el-input v-model="sysLogForm.interfaceUrl"></el-input>
								</el-form-item>
							</el-col>
							<el-col :md="8">
								<el-form-item label="操作人">
									<el-input v-model="sysLogForm.operateUserName"></el-input>
								</el-form-item>
							</el-col>
						</el-row>
						<el-row>
							<el-col :md="8">
								<el-form-item label="操作时间">
									<div style="display: flex; justify-content: space-between; justify-items: center;">
										<el-date-picker type="date" placeholder="开始日期" value-format="yyyy-MM-dd"
											v-model="sysLogForm.operateTimeStart" style="width:130px;"></el-date-picker>
										<div style="margin: 0 5px;"> ~ </div>
										<el-date-picker type="date" placeholder="结束日期" value-format="yyyy-MM-dd"
											v-model="sysLogForm.operateTimeEnd" style="width:130px;"></el-date-picker>
									</div>
								</el-form-item>
							</el-col>
							<el-col :md="16" class="search-btn">
								<div class="search-btn-right">
									<el-button type="primary" icon="el-icon-search" @click="handleSearch">查询</el-button>
									<el-button icon="el-icon-refresh" @click="resetForm">重置</el-button>
								</div>
							</el-col>
						</el-row>
					</el-form>
					<el-table :data="tableData" border stripe style="width: 100%">
						<el-table-column label="序号" width="60" header-align="left" align="center">
							<template
								slot-scope="scope">{{scope.$index + 1 + (tableCurrentPage-1)*tablePageSize}}</template>
						</el-table-column>
						<el-table-column label="操作类型" prop="operateType" min-width="60">
							<template slot-scope="scope">
								<span v-if="scope.row.operateType == '2'">移动</span>
								<span v-else>PC</span>
							</template>
						</el-table-column>
						<el-table-column label="操作接口" prop="interfaceUrl" min-width="150"></el-table-column>
						<el-table-column label="接口参数" prop="interfaceParam" min-width="200">
							<template slot-scope="scope">
								<div class="view-text" @click="showJsonDialog(scope.row.interfaceParam)">
									{{scope.row.interfaceParam}}
								</div>
							</template>
						</el-table-column>
						<el-table-column label="接口响应数据" prop="interfaceResponse" min-width="200">
							<template slot-scope="scope">
								<div class="view-text" @click="showJsonDialog(scope.row.interfaceResponse)">
									{{scope.row.interfaceResponse}}
								</div>
							</template>
						</el-table-column>
						<el-table-column label="操作人" prop="operateUserName" min-width="60"></el-table-column>
						<el-table-column label="操作时间" prop="operateTime" align="center" min-width="100"></el-table-column>
					</el-table>
					<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
						:current-page.sync="tableCurrentPage" :page-sizes="[10, 20, 30, 50]"
						:page-size.sync="tablePageSize" layout="total, jumper, prev, pager, next, sizes"
						:total="tableTotal"></el-pagination>
				</div>
			</el-scrollbar>
		</div>
		<el-dialog title="JSON数据" :visible.sync="dialogVisible">
			<div>
				<json-viewer :value="dialogContent" :expand-depth="5" copyable boxed sort expanded></json-viewer>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import JsonViewer from 'vue-json-viewer'
	export default {
		components: {
			JsonViewer
		},
		data() {
			return {
				sysLogForm: {
					operateType: "",
					content: "",
					operateUser: "",
					operateTime: ""
				},
				operateTypeList: [{
						id: "1",
						name: "PC"
					},
					{
						id: "2",
						name: "移动"
					}
				],
				tableTotal: 0,
				tableCurrentPage: 1,
				tablePageSize: 10,
				tableData: [],
				dialogVisible: false,
				dialogContent: {}
			};
		},
		mounted() {
			this.getTableData(this.tableCurrentPage, this.tablePageSize);
		},
		methods: {
			handleSizeChange(val) {
				this.getTableData(this.tableCurrentPage, val);
			},
			handleCurrentChange(val) {
				this.getTableData(val, this.tablePageSize);
			},
			handleSearch() {
				this.getTableData(this.tableCurrentPage, this.tablePageSize);
			},
			getTableData(page, pageSize) {

				let pageParams = '?page=' + page + '&rows=' + pageSize;

				this.$api.post('/log/findByPage' + pageParams, this.sysLogForm).then(
					response => {
						this.tableData = response.data.list;
						this.tableTotal = response.data.total;
					}
				);
			},
			resetForm() {
				this.sysLogForm = {
					logType: "",
					content: "",
					operateUser: "",
					operateTime: ""
				};
				this.getTableData(1, this.tablePageSize);
			},
			showJsonDialog(content) {
				this.dialogContent = JSON.parse(content);
				this.dialogVisible = true;
			}
		}
	};
</script>
